<script type="text/javascript" src="/ckeditor/ckeditor.js"></script>
<h1>Edit Villages</h1>

<?php include_partial('form', array('form' => $form)) ?>
  <form id="gMapsForm" action="#" onsubmit="showAddress(this.address.value); return false"> 
     <p>        
      <input type="text" size="60" name="address" value="<?php echo $form['nomVillage']->getValue() ?>" /> 
      <input type="submit" value="Search!" /> 
      </p>
<div align="center" id="map" style="width: 600px; height: 400px"><br/></div>	  
   </form> 
   
<script src="http://maps.google.com/maps?file=api&v=1&key=<?php echo sfConfig::get('app_googlemaps_key') ?>" type="text/javascript"></script> 
<script type="text/javascript"> 
 
 function load() {
      if (GBrowserIsCompatible()) {
        var map = new GMap2(document.getElementById("map"));
        map.addControl(new GSmallMapControl());
        map.addControl(new GMapTypeControl());
        var center = new GLatLng(<?php echo $form['geolatitud']->getValue() ?>, <?php echo $form['geolongitud']->getValue() ?>);
        map.setCenter(center, 15);
        geocoder = new GClientGeocoder();
        var marker = new GMarker(center, {draggable: true});  
        map.addOverlay(marker);
        document.getElementById("villages_geolatitud").value = center.lat().toFixed(5);
        document.getElementById("villages_geolongitud").value = center.lng().toFixed(5);
 
	  GEvent.addListener(marker, "dragend", function() {
       var point = marker.getPoint();
	      map.panTo(point);
       document.getElementById("villages_geolatitud").value = point.lat().toFixed(5);
       document.getElementById("villages_geolongitud").value = point.lng().toFixed(5);
 
        });
 
 
	 GEvent.addListener(map, "moveend", function() {
		  map.clearOverlays();
    var center = map.getCenter();
		  var marker = new GMarker(center, {draggable: true});
		  map.addOverlay(marker);
		  document.getElementById("villages_geolatitud").value = center.lat().toFixed(5);
	   document.getElementById("villages_geolongitud").value = center.lng().toFixed(5);
 
 
	 GEvent.addListener(marker, "dragend", function() {
      var point =marker.getPoint();
	     map.panTo(point);
      document.getElementById("villages_geolatitud").value = point.lat().toFixed(5);
	     document.getElementById("villages_geolongitud").value = point.lng().toFixed(5);
 
        });
 
        });
 
      }
    }
 
	   function showAddress(address) {
	   var map = new GMap2(document.getElementById("map"));
       map.addControl(new GSmallMapControl());
       map.addControl(new GMapTypeControl());
       if (geocoder) {
        geocoder.getLatLng(
          address,
          function(point) {
            if (!point) {
              alert(address + " not found");
            } else {
		  document.getElementById("villages_geolatitud").value = point.lat().toFixed(5);
	   document.getElementById("villages_geolongitud").value = point.lng().toFixed(5);
		 map.clearOverlays()
			map.setCenter(point, 14);
   var marker = new GMarker(point, {draggable: true});  
		 map.addOverlay(marker);
 
		GEvent.addListener(marker, "dragend", function() {
      var pt = marker.getPoint();
	     map.panTo(pt);
      document.getElementById("villages_geolatitud").value = pt.lat().toFixed(5);
	     document.getElementById("villages_geolongitud").value = pt.lng().toFixed(5);
        });
 
 
	 GEvent.addListener(map, "moveend", function() {
		  map.clearOverlays();
    var center = map.getCenter();
		  var marker = new GMarker(center, {draggable: true});
		  map.addOverlay(marker);
		  document.getElementById("villages_geolatitud").value = center.lat().toFixed(5);
	   document.getElementById("villages_geolongitud").value = center.lng().toFixed(5);
 
	 GEvent.addListener(marker, "dragend", function() {
     var pt = marker.getPoint();
	    map.panTo(pt);
    document.getElementById("villages_geolatitud").value = pt.lat().toFixed(5);
	   document.getElementById("villages_geolongitud").value = pt.lng().toFixed(5);
        });
 
        });
 
            }
          }
        );
      }
    }
	
	// BEGIN NEW CODE TO AVOID USING BODY TAG FOR LOAD/UNLOAD
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

addLoadEvent(load);

// arrange for our onunload handler to 'listen' for onunload events
if (window.attachEvent) {
        window.attachEvent("onunload", function() {
                GUnload();      // Internet Explorer
        });
} else {

        window.addEventListener("unload", function() {
                GUnload(); // Firefox and standard browsers
        }, false);

}
// END NEW CODE TO AVOID USING BODY TAG FOR LOAD/UNLOAD

    </script> 